package cn.edu.djtu.fw.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.djtu.fw.model.Admin;
import cn.edu.djtu.fw.model.Json;
import cn.edu.djtu.fw.service.AdminServiceI;
import cn.edu.djtu.fw.util.JsonUtil;

import com.opensymphony.xwork2.ActionSupport;

public class AdminAction extends ActionSupport implements ServletRequestAware {

	private static final long serialVersionUID = 1L;

	private String id;
	private String name;
	private String pwd;
	private HttpServletRequest request;
	private Admin admin;
	private int page;
	private int rows;
	private String ids;

	@Override
	public String execute() throws Exception {
		Map<String, Object> m = new HashMap<String, Object>();
		m = adminService.getAllAdmin(page, rows, admin);
		JsonUtil.writeJson(m);
		return null;
	}

	public String login() throws Exception {
		HttpSession session = request.getSession();
		Admin admin2 = new Admin();
		Json j = new Json();
		admin2 = adminService.login(admin);
		if (admin2 != null) {
			session.setAttribute("admin", admin2);
			j.setSuccess(true);
			j.setMsg("登录成功");
			JsonUtil.writeJson(j);
			return null;
		} else {
			j.setMsg("登录失败");
			JsonUtil.writeJson(j);
			return null;
		}
	}

	public String loginOut() throws Exception {
		HttpSession session = request.getSession();
		if (session != null && session.getAttribute("admin") != null) {
			session.invalidate();
			return SUCCESS;
		}
		return ERROR;
	}

	public void add() throws Exception {
		Json j = new Json();
		if (adminService.insert(admin)) {
			j.setSuccess(true);
			j.setMsg("添加成功！");
		} else {
			j.setMsg("添加失败！");
		}
		JsonUtil.writeJson(j);
	}

	public void delete() throws Exception {
		Json j = new Json();
		String[] idsStr = ids.split(",");
		for (int i = 0; i < idsStr.length; i++) {
			Admin a = adminService.getAdminById(Integer.parseInt(idsStr[i]));
			if (a != null) {
				adminService.delete(a.getId());
			}
		}
		j.setSuccess(true);
		j.setMsg("删除成功！");
		JsonUtil.writeJson(j);
	}

	public void update() throws Exception {
		Json j = new Json();
		if (adminService.update(admin)) {
			j.setSuccess(true);
			j.setMsg("修改成功！");
		} else {
			j.setMsg("修改失败！");
		}
		JsonUtil.writeJson(j);
	}

	private AdminServiceI adminService;

	public AdminServiceI getAdminService() {
		return adminService;
	}

	@Autowired
	public void setAdminService(AdminServiceI adminService) {
		this.adminService = adminService;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public Admin getAdmin() {
		return admin;
	}

	public void setAdmin(Admin admin) {
		this.admin = admin;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
}
